package com.seeyon.apps.thirdSso.handshake;

import com.seeyon.apps.thirdSso.util.HttpUtils;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.portal.sso.SSOLoginContext;
import com.seeyon.ctp.portal.sso.SSOLoginHandshakeAbstract;
import com.seeyon.ctp.util.Strings;
import com.seeyon.ctp.util.json.JSONUtil;
import lombok.SneakyThrows;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 描述：一体化平台跳转OA首页
 *  pc端打开的页面：pc端的链接地址
 *  移动端打开的页面：实际为h5页面，而不是app里面的页面
 * </p>
 *
 * @author: zhangshubin
 * @since: 2024/12/16 9:30
 * @description: 类描述
 */
public class IntegratedLoginHandShake extends SSOLoginHandshakeAbstract {
    private OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
    private static Log log = LogFactory.getLog(IntegratedLoginHandShake.class);
    public static String loginName = "";
    /**
     * 简单一点处理，传过来的ticket=登录名 + “_” +时间戳，然后base64编码一下
     * 获取到ticket后，base64解码一下即可拿到登录名
     * @param ticket
     * @return
     * http://192.168.52.228/seeyon/login/sso?from=ksy&ticket=
     */
    @SneakyThrows
    @Override
    public String handshake(String ticket) {
            try {

                log.info("进入单点登录接口----");
                //分割code和时间戳；
                String userAgentFrom = AppContext.getRawRequest().getParameter("UserAgentFrom");
                String userInfo = HttpUtils.getUserInfo(ticket,userAgentFrom);
                //使用code解析用户信息
                long memberIdByCode = HttpUtils.getMemberIdByCode(userInfo);
                log.info("获取用户ID:"+memberIdByCode);
                if (memberIdByCode!=0){
                    V3xOrgMember memberById = orgManager.getMemberById(memberIdByCode);
                    log.info("用户信息："+memberById);
                    loginName = memberById.getLoginName();
                    return loginName;
                }
            } catch (BusinessException e) {
                throw new BusinessException("ticket解析异常",e.getMessage());
            }
        return null;
    }

    @Override
    public void logoutNotify(String ticket) {
        System.out.println("退出系统接收到的ticket：" + ticket);
    }
    @Override
    public String getToUrl(HttpServletRequest req, SSOLoginContext ssoLoginContext, String ticket) {
        String url = req.getParameter("tourl");
        String type = req.getParameter("UserAgentFrom");
        if (type.equals("weixin")){
//            log.info("AppContext.getCurrentUser().getUserAgentFrom():"+AppContext.getCurrentUser().getUserAgentFrom());
            url = "/seeyon/H5/collaboration/index.html?token="+getToken(loginName)+"&html=/seeyon/m3/apps/v5/portal/html/portalIndex.html?weixinFrom=app&wxtmainportal=true";
//            url = "/seeyon/H5/wechat/html/allApps.html?token="+getToken(loginName);
        }
        log.info("单点登录跳转地址:"+url);
        // TODO Auto-generated method stub
        return url;
    }

    public static String getToken(String loginName) {
        try {
            //String str = HttpRequest.sendGet("https://117.73.2.125:88/seeyon/rest/token/rest/c4aa3027-5759-465f-9768-e3c8f67a68f9?loginName="+loginName);
            String str = HttpUtils.sendGet("http://172.17.190.19:99/seeyon/rest/token/rest/bfdfa0b2-c82b-4c1c-aac4-4ba0cb1fbfcd?loginName="+loginName+"&userAgentFrom=weixin");
            //String str = HttpRequest.sendGet("https://127.0.0.1:88/seeyon/rest/token/rest/c4aa3027-5759-465f-9768-e3c8f67a68f9?loginName="+loginName);
            Map map = JSONUtil.parseJSONString(str, Map.class);
            String  user = map.get("id").toString();
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


}
